<?php

namespace App\Http\Controllers;

use App\Http\Requests\IndexBlindActivityRequest;
use App\Http\Requests\StoreBlindActivityRequest;
use App\Http\Requests\UpdateBlindActivityRequest;
use App\Http\Resources\BlindActivityResource;
use App\Models\BlindActivity;
use Illuminate\Support\Facades\Auth;

class BlindActivityController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth:sanctum');
    }

    /**
     * Display a listing of the resource.
     */
    public function index(IndexBlindActivityRequest $request)
    {
        $query = BlindActivity::query();
        if (!isSuperadmin()) {
            $query->where('admin_id', auth()->id());
        }
        $query->when($request->filled('name'), function ($query) use ($request) {
            $name = $request->input('name');
            return $query->whereHas('activitys', function ($subquery) use ($name) {
                $subquery->where('name', 'like', "%$name%");
            });
        });

        $data = $query->with(['blinds.blindtypelabel', 'activitys', 'blinds.blindcates'])->orderBy('order')->paginate(
            $request->query('pageSize'),
            ['*'],
            'page',
            $request->query('pageNum')
        );
        $response =  BlindActivityResource::collection($data);
        $total = $response->total();
        return $this->success('查询成功', $response, $total);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreBlindActivityRequest $request, BlindActivity $blindActivity)
    {
        $blindActivity->fill($request->input());
        $blindActivity->admin_id = Auth::id();
        $blindActivity->save();
        return $this->success('添加成功', $blindActivity);
    }

    /**
     * Display the specified resource.
     */
    public function show(BlindActivity $blindActivity, $id)
    {
        $blindActivity = $blindActivity->findOrFail($id);
        $data = new BlindActivityResource($blindActivity->load(['blinds', 'activitys']));
        return $this->success('查询成功', $data);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(BlindActivity $blindActivity)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateBlindActivityRequest $request, BlindActivity $blindActivity, $id)
    {
        $blindActivity = $blindActivity->findOrFail($id);
        $this->authorize('update', $blindActivity);
        $blindActivity->fill($request->input());
        $blindActivity->admin_id = Auth::id();
        $blindActivity->save();
        return $this->success('更新成功', $blindActivity);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(BlindActivity $blindActivity, $id)
    {
        $blindActivity = $blindActivity->findOrFail($id);
        $this->authorize('delete', $blindActivity);
        $blindActivity->delete();
        return $this->success('删除成功', $blindActivity);
    }
}
